package 贪心算法;

import java.util.HashMap;

public class 连续的子数组和 {

    public static boolean checkSubarraySum(int[] nums, int k) {
        int n = nums.length;
        int[] sum  = new int[n];
        sum[0] = nums[0];
        for (int i = 1; i < n; i++) {
            sum[i] = sum[i-1]+nums[i];
        }
        HashMap<Integer,Integer> map = new HashMap();
        //防止下标0开头的子数组的和是k的倍数
        map.put(0,-1);
        for (int i = 0; i < n; i++) {
            int j = sum[i] % k;
            if (map.containsKey(j)){
                if (i - map.get(j) >=2){
                    return true;
                }
            }else {
                map.put(j,i);
            }
        }
        return false;
    }

    public static void main(String[] args) {
        int[] nums = {23,2,6,4,7};
        System.out.println(checkSubarraySum(nums,6));
    }
}
